#!/user/bin/even Python3
# -*- coding:utf-8 -*-
# GetExcelData.py
# 获取Excel数据
# author:zhaohexin
# time：2020/7/30 10:25

import itertools

from utility.Management import OpenExcel
from conf import SendHistory


class GetExcelData:
    def __init__(self):
        self.path = SendHistory.excel_path

    def get_data(self):
        """
        获取Excel数据并组装成所需格式
        :return:
        """
        datas = []
        with OpenExcel(self.path) as f:
            sheets = f.sheetnames
            for sheet in sheets:
                work = f[sheet]
                # 循环获取所有数据
                sheet_datas = self.get_sheet_data(work)
                [datas.append(data) for data in sheet_datas]
        return datas

    def get_sheet_data(self, work):
        """
        获取某一sheet页所有数据
        :work: 活动sheet页
        :return: 数据
        """
        rows = work.max_row
        columns = work.max_column
        datas = []
        for i in range(1, rows + 1):
            data = []
            for h in range(1, columns + 1):
                cell_value = work.cell(row=i, column=h).value
                data.append(cell_value)
            datas.append(data)
        # 转换字典
        key = datas.pop(0)
        datas_dict = []
        for data in datas:
            data_dict = {k: v for k, v in zip(key, data)}
            data_dict = {"ID": data_dict.get("ID"), "search_industry": data_dict.get("输入产品名称"), "search_career": data_dict.get("职业名称")}
            datas_dict.append(data_dict)
        return datas_dict


if __name__ == '__main__':
    GetExcelData().get_data()
